﻿#include "databuffer.h"
#include "utils/utils.h"

DataBuffer::DataBuffer(int len) {
    Data.resize(len);
    Data.clear();
    rd_index = 0;
    b_end = false;
}

DataBuffer::DataBuffer(QByteArray &buffer){
    Data.clear();
    rd_index = 0;
    append_bytes(buffer);
    b_end = false;
}

DataBuffer::DataBuffer(u8 *data, int len){
    Data.clear();
    rd_index = 0;
    append_buff(data,len);
    b_end = false;
}

DataBuffer::DataBuffer(DataBuffer *db){
    Data.clear();
    rd_index = db->index();
    b_end = db->end();
    Data.append(db->byte_array());
}

DataBuffer::~DataBuffer() {
    Data.clear();
    Data.squeeze();
}

void DataBuffer::append_s32(int32_t data) {
    u8 buff[4];
    Utils::encode_s32(buff,data);
    Data.append((char *)buff, sizeof(buff));
}
void DataBuffer::append_s16(int16_t data) {
    u8 buff[2];
    Utils::encode_s16(buff,data);
    Data.append((char *)buff, sizeof(buff));
}
void DataBuffer::append_s8(int8_t data) {
    Data.append((char *)&data, sizeof(data));
}
void DataBuffer::append_u32(uint32_t data) {
    u8 buff[4];
    Utils::encode_u32(buff,data);
    Data.append((char *)buff, sizeof(buff));
}

void DataBuffer::append_u24(uint32_t data){
    u8 buff[3];
    Utils::encode_u24(buff,data);
    Data.append((char *)buff, sizeof(buff));
}

void DataBuffer::append_u16(uint16_t data) {
    u8 buff[2];
    Utils::encode_u16(buff,data);
    Data.append((char *)buff, sizeof(buff));
}
void DataBuffer::append_u8(uint8_t data) {
    Data.append((char *)&data, sizeof(data));
}
void DataBuffer::append_buff(uint8_t *buff, int len) {
    Data.append((char *)buff, len);
}
void DataBuffer::append_bytes(QByteArray &data) {
    Data.append(data);
}

void DataBuffer::append_float(float data) {
    u32 *p = (u32 *)&data;
    u32 u_data = *p;
    append_u32(u_data);
}

u8 DataBuffer::value_u8(int index) {
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(1);
    return Utils::decode_u8(data);
}
u16 DataBuffer::value_u16(int index) {
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(2);
    return Utils::decode_u16(data);
}

u32 DataBuffer::value_u24(int index){
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(3);
    return Utils::decode_u24(data);
}

u32 DataBuffer::value_u32(int index) {
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(4);
    return Utils::decode_u32(data);
}
s8 DataBuffer::value_s8(int index) {
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(1);
    return Utils::decode_s8(data);
}
s16 DataBuffer::value_s16(int index) {
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(2);
    return Utils::decode_s16(data);
}
s32 DataBuffer::value_s32(int index) {
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(4);
    return Utils::decode_s32(data);
}

float DataBuffer::value_float(int index){
    u8 *data = (u8 *)Data.data() + rd_index + index;
    skip(4);
    return Utils::decode_float(data);
}

void DataBuffer::skip(int offset) {
    rd_index += offset;
    int index = rd_index % Data.size();
    if (index != rd_index) {
        b_end = true;
    }
    rd_index = index;
}

